<?php
namespace app\index\controller;
use app\index\logic\VisitorRecordLogic;

/**
 * 微信用户访问记录api控制器
 * Created by PhpStorm.
 * User: tyll
 * Date: 2020/04/10
 * Time: 16:13
 */
class VisitorRecord extends Base {

    public function __construct(){
        parent::__construct();
        $this->vr_logic = new VisitorRecordLogic();
    }

    /**
     * 上报微信用户访问记录
     * @return \think\response\Json
     */
    public function ajax_add_vr(){
        $params = input();
        $r = $this->check_user_token($params);
        if($r['code'] != 1){
            return json($r);
        }
        $openid = session('openid');

        $maps = [];
        $maps[] = ['openid','=',$openid];
        $vr_detail = model('visitor_record')->get_detail($maps,'id');

        $vr_data = [];
        $vr_data['openid'] = $openid;
        $vr_data['is_first'] = empty($vr_detail) ? 1 : 0;
        $vr_data['mini_page'] = $params['mini_page'];
        $vr_data['mini_params'] = $params['mini_params'];
        $r = model('visitor_record')->insert($vr_data);

        if($r){
            return json($this->vr_logic->get_response(1,'上报成功'));
        }
        return json($this->vr_logic->get_response(0,'上报失败'));
    }
}
